/**
 * @Author: 安广伟
 * @Email: anguangwei@jwzh.tech
 * @Date: 2024-09-09 13:51:53
 * @LastEditTime 2024-09-09 13:51:53
 * @since: 1.0.0
 * @Version: 1.0.0
 * @Description: swagger2配置
 */

package com.ry.mall.server.config;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Configuration
public class SwaggerConfig {

    /**
     * 根据@Tag 上的排序，写入x-order 这个自定义配置必须存在，否则无法定制 knife4j 的 web 首页
     *
     * @return the global open api customizer
     */
    @Bean
    public GlobalOpenApiCustomizer orderGlobalOpenApiCustomizer() {
        return openApi -> {
            if (openApi.getTags() != null) {
                openApi.getTags().forEach(tag -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("x-order", new Random().nextInt(0, 100));
                    tag.setExtensions(map);
                });
            }
            if (openApi.getPaths() != null) {
                openApi.addExtension("x-mall", "mall");
                openApi.getPaths().addExtension("x-abb", new Random().nextInt(1, 100));
            }
        };
    }

    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI().info(new Info().title("瑞谊 Mall Application Server API").version("1.0.0")
                .contact(new Contact().name("徐泽宇").email("roamerxv@gmail.com"))
                .description("瑞谊 Mall Application Server API").termsOfService("http://ruiyi.sh.cn")
                .license(new License().name("Apache 2.0").url("http://ruiyi.sh.cn")));
    }
}
